package com.xcgame.oh;

import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.os.Environment;
import android.util.Log;

/**
 * 下载文件请求回调
 * <br/>
 * Created by 小灿 on 2016/11/14.
 */
public class DownloadResponseCallback {

    private Context context;
    private String url;

    public Context getContext() {
        return context;
    }

    public String getUrl() {
        return url;
    }

    public void onStart() {
        if (OkHttp.isPrintLog) {
            Log.d(OkHttp.TAG, "文件下载开始：" + url);
        }
    }

    public void onFailure(int statusCode) {
        if (OkHttp.isPrintLog) {
            Log.d(OkHttp.TAG, "文件下载失败" + statusCode);
        }
    }

    public void onSuccess(String filePath) {
        if (OkHttp.isPrintLog) {
            Log.d(OkHttp.TAG, "文件下载成功：" + filePath);
        }
    }

    public void onCancel() {
        if (OkHttp.isPrintLog) {
            Log.d(OkHttp.TAG, "文件下载取消：" + url);
        }
    }

    public void onFinish() {
        if (OkHttp.isPrintLog) {
            Log.d(OkHttp.TAG, "文件下载结束：" + url);
        }
    }

    public void onProgress(int progress) {
        if (OkHttp.isPrintLog) {
            Log.d(OkHttp.TAG, "文件下载进度：" + progress);
        }
    }

    public void isNotOpenNetwork() {
        if (OkHttp.isPrintLog) {
            Log.d(OkHttp.TAG, "没有打开网络连接！");
        }
    }

    protected void setUrl(String url) {
        this.url = url;
    }

    protected void setContext(Context context) {
        this.context = context;
    }

    /**
     * 获取存放下载目录路径
     *
     * @return
     */
    protected String getTargetDirPath(Context context) {
        String targetDirPath = Environment.getExternalStorageDirectory() + "/" + getApplicationName(context) + "/download";
        return targetDirPath;
    }

    /**
     * 获取应用名称
     *
     * @return
     */
    private String getApplicationName(Context context) {
        PackageManager packageManager = null;
        ApplicationInfo applicationInfo;
        try {
            packageManager = context.getPackageManager();
            applicationInfo = packageManager.getApplicationInfo(context.getPackageName(), 0);
        } catch (PackageManager.NameNotFoundException e) {
            applicationInfo = null;
        }
        String applicationName = (String) packageManager.getApplicationLabel(applicationInfo);
        return applicationName;
    }
}
